<?php
class coffee_shop extends core {
	public function __construct() {
		parent::__construct();
	}

	//index
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= "AND (c.name LIKE '%{$keyword}%' OR c.name_english LIKE '%{$keyword}%' OR c.memo LIKE '%{$keyword}%')";
		}
		if ($id) {
			$where .= "AND c.id='{$id}'";
		}
		//分页
		$this->setpages(array('id'=>$id, 'keyword'=>$keyword));

		$sql = "SELECT c.* , s.name as shop_name FROM jf_coffee_shop c LEFT JOIN jf_shop s ON c.shop_id = s.id 
				WHERE 1 {$where} ORDER BY c.id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);

		$this->display();
	}

	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$sql = "SELECT * FROM jf_shop WHERE 1  ORDER BY id DESC";
			$shop = $this->db->get_results($sql);
			$this->smarty->assign('shop',$shop);
			$this->display();
		}
	}	

	//add
	public function edit() {
		if (IS_POST) { //添加
			$this->save();
		} else { //显示
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM jf_coffee_shop WHERE id = '{$id}' ORDER BY id";
			$row = $this->db->get_row($sql);
			$sql = "SELECT * FROM jf_shop WHERE 1  ORDER BY id DESC";
			$shop = $this->db->get_results($sql);
			$this->smarty->assign('shop',$shop);
			$this->smarty->assign('row', $row);
			$this->display();
		}
	}
	
	//写入到数据库
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : 0;
		$mobile = (isset($_POST['tel']) && trim($_POST['tel'])) ? trim($_POST['tel']) : 0;
		$shop_id = (isset($_POST['shop_id']) && intval($_POST['shop_id'])) ? intval($_POST['shop_id']) : 0;
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;

		$pic = upload_one_file("coffee_shop","pic");
		if ($id > 0) {  //edit
			$sql = "UPDATE jf_coffee_shop SET name='{$name}', mobile='{$mobile}', shop_id='{$shop_id}' , 
	                 status='{$status}'  WHERE id='{$id}'";
			$act = $this->db->query($sql);
			//更新图片
			if($pic){
				$sql = "UPDATE jf_coffee_shop SET pic='{$pic}' where id = '{$id}'";
				$this->db->query($sql);
			}
		} else { //add
			$sql = "INSERT INTO jf_coffee_shop(name, mobile, shop_id ,status,pic,add_time) 
					VALUES('{$name}', '{$mobile}', '{$shop_id}', '{$status}','{$pic}', '".time()."')";
			$this->db->query($sql);
		}
		header("Location:?app=coffee_shop&act=index");
	}

	//delete
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM jf_coffee_shop WHERE id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=coffee_shop&act=index");
	}
}
